package com.tdtxdcxm.mymathdraw.slice;

import com.tdtxdcxm.mymathdraw.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import parsii.eval.Expression;
import parsii.eval.Parser;
import parsii.eval.Scope;
import parsii.eval.Variable;
import parsii.tokenizer.ParseException;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {

    DirectionalLayout rootdl_stkl_ddl2,rootdl_stkl_ddl2_set;
    Button topdl_but;

    TextField bottomdl_tfd;

    Text topdl_txt,paintswitch_text,uiswitch_text;
    Switch switch_paintcolor,switch_uicolor;

    Paint bluepaint = new Paint();
    Paint purplepaint = new Paint();

    Paint paint = bluepaint;//用于绘制任务的默认画笔（蓝色）
    int origin_x,origin_y;

    boolean isgeneratexy = false;

    public void initPaint(){
        bluepaint.setColor(Color.BLUE);
        bluepaint.setStrokeWidth(8);

        purplepaint.setColor(new Color(Color.rgb(160, 32, 240)));
        purplepaint.setStrokeWidth(8);
    }

    public void initMASComponents(){
        topdl_txt = (Text) findComponentById(ResourceTable.Id_topdl_txt);
        topdl_but = (Button) findComponentById(ResourceTable.Id_topdl_but);
        bottomdl_tfd = (TextField) findComponentById(ResourceTable.Id_bottomdl_tfd);


        rootdl_stkl_ddl2 = (DirectionalLayout) findComponentById(ResourceTable.Id_rootdl_stkl_ddl2);
        rootdl_stkl_ddl2_set = (DirectionalLayout) findComponentById(ResourceTable.Id_rootdl_stkl_ddl2_set);

        paintswitch_text = (Text) findComponentById(ResourceTable.Id_paintswitch_text);
        uiswitch_text = (Text) findComponentById(ResourceTable.Id_uiswitch_text);

        switch_paintcolor = (Switch) findComponentById(ResourceTable.Id_switch_paintcolor);
        switch_uicolor = (Switch) findComponentById(ResourceTable.Id_switch_uicolor);

        bottomdl_tfd.setHint("输入函数>>>"+"\n【右上角表情】：\n1.单击—绘制"+"\n2.长按-清空");


        topdl_txt.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                bottomdl_tfd.clearFocus();
                rootdl_stkl_ddl2.setVisibility(Component.VISIBLE);
                if(topdl_but.getText().equals("😁")){
                    topdl_but.setText("🤣");
                }
            }
        });

        topdl_but.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                bottomdl_tfd.clearFocus();
                String function = bottomdl_tfd.getText();

                ArrayList<float[]> xylist = generateXY(function);//根据表达式生成许多x、y点坐标
                if(xylist == null){
                    return;
                }
                bottomdl_tfd.addDrawTask(new Component.DrawTask() {
                    @Override
                    public void onDraw(Component component, Canvas canvas) {
                        if(isgeneratexy) {
                            for (float[] xyfloats : xylist) {
                                canvas.drawPoint(xyfloats[0], xyfloats[1], paint);
                            }
                        }
                    }
                }, Component.DrawTask.BETWEEN_BACKGROUND_AND_CONTENT);
            }
        });
        topdl_but.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                bottomdl_tfd.setText("");
                bottomdl_tfd.clearFocus();
                bottomdl_tfd.addDrawTask(new Component.DrawTask() {
                    @Override
                    public void onDraw(Component component, Canvas canvas) {
                        //用于清空，画一个空白
                    }
                }, Component.DrawTask.BETWEEN_BACKGROUND_AND_CONTENT);
            }
        });


        rootdl_stkl_ddl2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //弹出设置面板后，单击其余区域会退出（收回）设置状态
                rootdl_stkl_ddl2.setVisibility(Component.HIDE);
                topdl_but.setText("😁");
            }
        });
        rootdl_stkl_ddl2_set.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //这里用来“吸收”单击事件，否则当单击设置面板时，也会退出设置状态
            }
        });


        switch_paintcolor.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                if(b == true){
                    paint = purplepaint;//把紫色画笔给绘制任务画笔
                    paintswitch_text.setText("画笔：紫色");
                    paintswitch_text.setTextColor(new Color(Color.rgb(160, 32, 240)));
                }
                else{
                    paint = bluepaint;//把蓝色画笔给绘制任务画笔，还原默认
                    paintswitch_text.setText("画笔：蓝色");
                    paintswitch_text.setTextColor(Color.BLUE);
                }
            }
        });
        switch_uicolor.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                if(b == true){
                    uiswitch_text.setText("界面：黑暗");
                    uiswitch_text.setTextColor(new Color(Color.rgb(54, 54, 54)));


                    ShapeElement shape = new ShapeElement();
                    shape.setRgbColor(new RgbColor(105,105,105));
                    bottomdl_tfd.setBackground(shape);

                }
                else{
                    uiswitch_text.setText("界面：明亮");
                    uiswitch_text.setTextColor(new Color(Color.rgb(238, 99, 99)));

                    ShapeElement shape = new ShapeElement();
                    shape.setRgbColor(new RgbColor(255,255,255));
                    bottomdl_tfd.setBackground(shape);
                }
            }
        });
    }

    public ArrayList<float[]> generateXY(String function){
        if(function.equals("")){
            return null;
        }
        origin_x = bottomdl_tfd.getWidth() / 2;//将绘图坐标远点的x设定在此
        origin_y = bottomdl_tfd.getHeight() / 2;//将绘图坐标远点的y设定在此
        System.out.println(origin_x);
        System.out.println(origin_y);


        ArrayList<float[]> xylist = new ArrayList<>();

        StringBuilder stringBuilder = new StringBuilder(function);
        Scope scope = new Scope();
        Variable var_x = scope.getVariable("x");
        Expression expression = null;
        try {
            expression = Parser.parse(function, scope);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(expression == null){
            return null;
        }

        for(float x = -400f;x <= 400.0f;x = x + 0.001f){
            var_x.setValue((double) x);
            double y = expression.evaluate();
            //将图形默认放大
            float[] xyfloats = {x*140+origin_x,-140*((float) y)+origin_y};

            xylist.add(xyfloats);
        }
        isgeneratexy = true;
        return xylist;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initPaint();
        initMASComponents();

    }

    @Override
    public void onInactive() {
        super.onInactive();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onBackground() {
        super.onBackground();
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}
